<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>类和对象</title>
</head>
<body>
    <script>
        // ES5 
        // 构造函数：用来产生对象的函数
        // function People(name,age){
        //     this.name = name;
        //     this.age = age;
        //     this.intro = function(){
        //         console.log(`我是${this.name}，今年${this.age}岁`);
        //     }
        // }
        // ES6提供了类(class)来描述对象
        class People{
            _gender = "男"
            // ES6中的构造函数
            constructor(name,age){
                this._name = name;
                this._age = age;
            }
            intro(){
                console.log(`我是${this._name}，今年${this._age}岁，
                性别为${this._gender}，我的手机号为：${this._phone}`);
            }
            get age(){
                console.log("访问age属性");
                return this._age;
            }
            set age(_age){
                console.log("修改age属性");
                if(_age >= 18 && _age <= 30){
                    this._age = _age;
                }
            }
            get name(){
                return this._name;
            }
            set name(_name){
                this._name = _name;
            }
        }
        // 通过new关键字调用类的构造函数来创建对象
        let p = new People("张飞",20);
        // console.log(p.name,p.age);
        // p.gender = "女";
        // p.phone = "13888888888";
        // p.intro();
        // let p1 = new People("关羽",25);
        // p1.intro();
        // let p2 = p;

        // p.age = 40;
        // p.intro();
        console.log(p.age);
        p.age = 28;
        p.intro();
    </script>
</body>
</html>